package com.pactera.widget;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;

public class DownloadManageAys extends AsyncTask<Void, Object, Void> {
	private static DownloadManageAys dd = new DownloadManageAys();
	// 保存下载通道以及正在加载的数据
	private List<DownloadInfo> _dis = new ArrayList<DownloadInfo>();
	static {
		if (dd.getStatus() != AsyncTask.Status.RUNNING) {
			dd.execute();
		}
		// 清除临时文件以及过期文件
		init();
	}
	private BlockingQueue<DownloadInfo> dis = new ArrayBlockingQueue<DownloadInfo>(
			2);

	public static DownloadManageAys getInstance() {
		return dd;
	}

	/**
	 * 清除临时文件以及过期文件
	 */
	private static void init() {

	}

	public void startDownload(DownloadInfo tm) {
		if (!verfityDownload(tm)) {
			try {
				dis.put(tm);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 是否存在于下载队列中
	 * 
	 * @param tm
	 * @return
	 */
	private boolean verfityDownload(DownloadInfo tm) {
		for (DownloadInfo _tm : _dis) {
			if (_tm != null && _tm.equals(tm)) {
				_tm.getNivs().addAll(tm.getNivs());
				return true;
			}
		}
		return false;
	}

	@Override
	protected Void doInBackground(Void... params) {
		while (true) {
			DownloadInfo di = null;
			try {
				di = dis.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
				continue;
			}
			if (di == null) {
				continue;
			}
			// modified by yfh
			File file = new File(di.getLocalpath());
			// System.out.println("file:" + di.getLocalpath());
			if (!file.exists()) {
				_dis.add(di);
				Thread _t = new Thread(new DownloadRunnable(di,
						new DownloadImpl()));
				_t.start();
			} else {
				// Drawable d = Drawable.createFromPath(di.getLocalpath());
				Drawable d = ImageLoaderWithCache.loadDrawableImage(di);
				publishProgress(di, d);
			}
			// modified by yfh
		}
	}

	@Override
	protected void onProgressUpdate(Object... values) {
		if (values.length == 2) {
			DownloadInfo dLoadInfo = (DownloadInfo) values[0];
			Drawable drawable = (Drawable) values[1];
			List<NetImageView> _nivs = dLoadInfo.getNivs();
			for (NetImageView _niv : _nivs) {
				if (_niv != null) {
					if (dLoadInfo.getImageCallback() != null) {
						if (dLoadInfo.getImageCallback().beforeDraw(drawable) != null) {
							_niv.setImageDrawable(dLoadInfo.getImageCallback()
									.beforeDraw(drawable));
						}
					} else {
						_niv.setImageDrawable(drawable);
					}
				}
			}
		}
		super.onProgressUpdate(values);
	}

	public class DownloadImpl implements DownloadRunnable.DownloadCallback {
		@Override
		public void callback(DownloadInfo dLoadInfo, int status) {
			_dis.remove(dLoadInfo);
			// publishProgress(dLoadInfo, status);
			switch (status) {
			case DownloadRunnable.STATUS_SUCCESS:
				// Drawable d =
				// Drawable.createFromPath(dLoadInfo.getLocalpath());
				Drawable d = ImageLoaderWithCache.loadDrawableImage(dLoadInfo);
				publishProgress(dLoadInfo, d);
				break;
			case DownloadRunnable.STATUS_FAIL:
				// startDownload(dLoadInfo);
				break;
			default:
				break;
			}
		}
	}
}
